<?php

/**
 * Account model
 *
 * Represents a single account entry.
 * 
 * @package    Euso
 * @subpackage Model
 */
class Account_Model_Account
{
	protected $_id = null;
	protected $_idname = null; // slug like
	protected $_nicename = null;
	protected $_email = null;
	protected $_code = null;

	protected $_salt = null;


    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }


	public function setId($accountId = null)
	{
		$this->_id = $accountId;
		return $this;
	}
	public function setIdName($idname = null)
	{
		$this->_idname = $idname;
		return $this;
	}
	public function setNicename($nicename = null)
	{
		$this->_nicename = $nicename;
		return $this;
	}
	public function setEmailAddress($email = null)
	{
		$this->_email = $email;
		return $this;
	}
	public function setCode($code = null)
	{
		$this->_code = $code;
		return $this;
	}
	public function setSalt($salt = null)
	{
		$this->_salt = $salt;
		return $this;
	}

	public function getId()
	{
		return $this->_id;
	}
	public function getIdName()
	{
		return $this->_idname;
	}
	public function getNicename()
	{
		return $this->_nicename;
	}
	public function getEmailAddress()
	{
		return $this->_email;
	}
	public function getCode()
	{
		return $this->_code;
	}
	public function getSalt()
	{
		return $this->_salt;
	}



    public function __set($name, $value)
    {
        $method = 'set' . ucfirst($name);
        $this->$method($value);
    }
    public function __get($name)
    {
        $method = 'get' . ucfirst($name);
        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }



	protected static $_instanceClassName = 'Account_Model_Account';
	protected static $_instanceList = array();



	public static function setInstanceClassName($className)
	{
		self::$_instanceClassName = $className;
	}

	/**
	 * Queries for a loaded instance.
	 */
	public static function findInstanceById($accountId)
	{
		if (intval($accountId) > 0 && isset(self::$_instanceList[$accountId]) === TRUE) {
			return self::$_instanceList[$accountId];
		}
		return NULL;
	}
	public static function findInstanceByEmailAddress($emailAddress)
	{
		if (empty($emailAddress) === FALSE) {
			$emailAddress = strtolower($emailAddress);
			foreach (self::$_instanceList as $inst){
				if ($inst->getEmailAddress() === $emailAddress) {
					return $inst;
				}
			}
		}
		return NULL;
	}
	public static function findInstanceByIdName($idname)
	{
		if (empty($idname) === FALSE) {
			$idname = strtolower($idname);
			foreach (self::$_instanceList as $inst){
				if ($inst->getIdName() === $idname) {
					return $inst;
				}
			}
		}
		return NULL;
	}

	/**
	 * Queries for an instance. If the instance is not found, it will be loaded from the database.
	 */
	public static function getInstanceById($accountId)
	{
		return self::getInstance('id', $accountId);
	}
	public static function getInstanceByEmailAddress($emailAddress)
	{
		return self::getInstance('email', $emailAddress);
	}
	public static function getInstanceByIdName($idname)
	{
		return self::getInstance('idname', $idname);
	}
	// The key is either 'id' or 'idname' or 'email'
	public static function getInstance($key, $value)
	{
		if ($key === 'id') {
			$instance = self::findInstanceById($value);
		} else if ($key === 'email') {
			$value = strtolower($value);
			$instance = self::findInstanceByEmailAddress($value);
		} else if ($key === 'idname') {
			$instance = self::findInstanceByIdName($value);
			$value = strtolower($value);
		} else {
			return NULL;
		}

		if ($instance != NULL) {
			return $instance;
		}


		$table = Account_Model_DbTable_Account::getInstance();

		$select = $table->select();
		$select
			->where($key .'= ?', $value)
			->where('active=1')
		;

		$dbResult = $table->fetchAll($select);

		if ($dbResult->count() !== 1) {
			return NULL;
		}

		$dbRow = $dbResult->getRow(0);


		$className = Euso_ObjectClass::getInstanceClassName('account');
		if (empty($className)) {
			$className = 'Account_Model_Account';
		}
		$instance = new $className();
		$instance
			->setId($dbRow->id)
			->setIdName($dbRow->idname)
			->setNicename($dbRow->nicename)
			->setEmailAddress($dbRow->email)
			->setCode($dbRow->code)
			->setSalt($dbRow->salt)
		;
		self::$_instanceList[$dbRow->id] = $instance;

		return $instance;
	}

	public static function getNewests($count = 10)
	{
		$instances = array();


		$table = Account_Model_DbTable_Account::getInstance();

		//TODO: filters
		$select = $table->select();
		$select
			->order('register_time DESC')
			->where('active=1')
			->limit($count)
		;

		$dbResult = $table->fetchAll($select);

		foreach ($dbResult as $dbRow) {
			if (isset(self::$_instanceList[$dbRow->id])) {
				$instances[] = self::$_instanceList[$dbRow->id];
			} else {
		$className = Euso_ObjectClass::getInstanceClassName('account');
		if (empty($className)) {
			$className = 'Account_Model_Account';
		}
		$instance = new $className();
				$instance
					->setId($dbRow->id)
					->setIdName($dbRow->idname)
					->setNicename($dbRow->nicename)
					->setEmailAddress($dbRow->email)
					->setCode($dbRow->code)
					->setSalt($dbRow->salt)
				;

				self::$_instanceList[$dbRow->id] = $instance;
				$instances[] = $instance;
			}
		}

		return $instances;
	}

	//TODO
	public static function getInstanceListById(array $idArray = NULL)
	{
	}
	public static function getInstanceListByIdName(array $idnameArray = NULL)
	{
	}
}